<?php

/**
 * This function create a page navigator of the list
 * @param  $page - The Posted/Get value of the current page
 * @param  $row_total - The total number of records of the list
 * @param  $page_size - The number of records displayed on each page
 * @param  $pagegroup_size - The number of page indexes displayed. Others will be visible when click on "Next xx pages" eg.
 * @param  $class - The class used in the page number
 * @param  $url - The navigation url
 * @return a table of navigation numbers for pages
 * @example $pages = pagenavigator(2,100,20,10,"$PHP_SELF?keyword=$keyword");
 * @author : pavennt
 */
function pagenavigator($page, $row_total, $page_size, $pagegroup_size,$url) 
{
	$andpage=(strrpos ($url,"?")===false)?"?p=":"&p=";
	$page_total = floor(($row_total-1)/$page_size)+1;
	if(!$page || $page > $page_total || $page < 1)
	{
		$page = 1;
	}
	
	$group = floor(($page-1)/$pagegroup_size)+1;
	$start_page = (($group-1)*$pagegroup_size)+1;
	$end_page = $start_page+$pagegroup_size-1;
	if ($end_page>$page_total) 
	{
		$end_page = $page_total;
	}
	if ($page_total>1) 
	{
		$str='';
		if ($end_page>$pagegroup_size)
		{
			$start_group = $pagegroup_size;
		}
		else
		{
			$start_group = 0;
		}
		if ($page>2)
		{
			$str.= '<a href="'.$url.'&p=1" >Trang đầu</a>';
		}
		if ($group>1)
		{
			$str.= '<a href="'.$url.'&p='.($page-1).'" > &lt;&lt;  </a>';
		}
		if($page!=1&&$page!=$page_total) {$str.= '<a href="'.$url.'&p='.($page-1).'" > &lt;&lt; </a>';}
		for ( $i=$start_page ; $i <= $end_page ;$i++)
		{
			$j=$i+$page_size;
			$begin = ($i-1)*$page_size + 1;
			$end = $begin + $page_size - 1; 
			if ($i == $page)
			{
				$str.= "<a class='current'>$i</a>";
			}
			else
			{
				$str.= '<a href="'.$url.'&p='.$i.'"> '.$i.'</a> ';
			}
		}
		if ($page_total-$end_page>$pagegroup_size)
		{
			$end_group = $pagegroup_size;
		}
		else
		{
			$end_group = $page_total-$end_page;
		}
		if ($page<$page_total)
		{
			$str.= '<a href="'.$url.'&p='.($page+1).'" > &gt;&gt; </a>';
		}
		if ($page_total>2&&$page<$page_total)
		{
			$str.= '<a  href="'.$url.'&p='.$page_total.'" >Trang cuối</a>';
		}
		/*$onchange = "onchange = 'window.location.href=".'$url&p=3'."'";
		$slt ="<select name='pagesoption' style='margin:0px 0px 0px 10px; padding:5px;' $onchange >";
		for($j=1; $j<=$page_total; $j++){
			
			if($j==$page){
				$slt .="<option value='$j' selected='selected'>".$j."</option>";
			}else{
				$slt .="<option value='$j'>".$j."</option>";
			}
		}
		$slt .="</select>";*/
		return $str;
	}	
}

function _POST($value='')
{
	global $_GET, $_POST, $HTTP_POST_VARS,$HTTP_GET_VARS;
	if (isset($_POST["$value"]))
	{
		return convertPost(str_replace("'","&#039;",$_POST["$value"]));
	}
	elseif (isset($HTTP_POST_VARS["$value"]))
	{
	    return convertPost(str_replace("'","&#039;",$HTTP_POST_VARS["$value"]));
	}
	elseif (isset($_GET["$value"]))
	{
	    return convertPost(str_replace("'","&#039;",$_GET["$value"]));
	}
	elseif (isset($HTTP_GET_VARS["$value"]))
	{
	    return convertPost(str_replace("'","&#039;",$HTTP_GET_VARS["$value"]));
	}
	else
	{
	    return ;
	}
}
/*
Name: converPost;
action:	Xoa bo cac ky tu trong lien nhau, ky tu trong o dau string va cuoi string
@author: Tuanlm
@result:string;
*/
function convertPost($str){
	if(is_array($str)){
		foreach ($str as $key => $value){
			$str[$key]=convertPost($str[$key]);
		}
	}
	else {
		if($str=="") return $str;
		$tem=array();
		$tem=explode(' ',$str);
		$n=count($tem);
		for($i=0;$i<$n;$i++){
			if($tem[$i]==""||$tem[$i]=="&nbsp;") unset($tem[$i]);
		}
		$str=implode(" ",$tem);
	}
	$str=str_replace("\\",'',$str);
	$str=str_replace("<?","",$str);
	$str=str_replace("<%","",$str);
	$str=str_replace("&lt;?","",$str);
	$str=str_replace("&lt;%","",$str);
	$str=str_replace("<script","",$str);
	$str=str_replace("&lt;script","",$str);
	return $str;
}